angular.module('service.ssouser', [
        "ngResource"
    ])
    .service('UserinfoService', UserinfoService);
UserinfoService.$inject = ['$resource',"$cookies"];
function UserinfoService($resource,$cookies) {
    // 微信openid登录
    this.wxUserLogin = function (openid) {
        return this.getUserinfoByOpenid(mall_userauth.wxmp_appid, openid);
    };
    //微信账号统一登录校验
    this.accountCheck = function(wxUser,success_func,error_func){
        if(!wxUser){
            return;
        }
        var _self = this;
        var loginuser = {
            username:wxUser.openid?wxUser.openid:wxUser,
            password:mall_userauth.wxmp_appid,
            acctype:4
        };
        var resource = $resource(sso_baseurl + 'login', {}, {
            login: {
                method: 'POST',
                //dataType: 'JSONP',//here
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: function (data, headersGetter) {
                    var str = [];
                    for (var d in data) {
                        str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
                    }
                    return str.join("&");
                }
            }
        });
        var login_ret = resource.login({}, loginuser,success_func,error_func);
        return login_ret;
    };

    // 微信openid注册
    this.wxUserRegister = function (openid, name, avatar,user) {
        return this.addUserinfoFromWeixin(mall_userauth.wxmp_appid, openid, name, avatar,user);
    };

    // 获取微信用户信息
    this.getUserinfoByOpenid = function (openid) {
        var UserInfo = $resource(sso_baseurl + 'user/wxuser/:openid');
        return UserInfo.get({ name: openid });
    };
    // 添加微信用户信息
    this.addUserinfoFromWeixin = function (appid, openid, name, avatar,user) {
        var UserWxregister = $resource(sso_baseurl + 'user/wxregist/:phone');
        var _self = this;
        var registeruser = {
            appid: appid,
            unionid: openid,
            //unionid: null,
            nickname: name,
            headimgurl: avatar,
        };
        if(user){
            return UserWxregister.save({phone:user.phone,code:user.code},registeruser,function(userinfo){
                _self.judgeWmUser(appid, openid, name, avatar,user,userinfo.userid);
            },function(error){
                if(error.data.reason){
                    var reason = JSON.parse(error.data.reason);
                    alert(reason.msg);
                }
            });
        }
        return null;
    };
    //判断统一登录用户外卖平台是否存在不存在添加新用户
    this.judgeWmUser = function(appid, openid, name, avatar,user,userid){
        var uc_user = $resource(uc_baseurl+"user/:userid");
        //p判断外卖是否有用户没有用户添加一个新的用户
        var self = this;
        var xwmuser;
        uc_user.get({userid:userid},function(data){
            data.phone = user.phone;
            data.logo = avatar;
            data.nickname = name;
            self.update(data);
        },function(){
            xwmuser = {
                "name": openid,
                "password": appid,
                "logo": avatar,
                "phone": user.phone,
                "nickname": name,
                "outerid":10001,
                "sign": 11
            };
            var xwmRegist = $resource(uc_baseurl+'user/add');
            xwmRegist.save({userid:userid},xwmuser,function(user){},function(){});
        });
    };
    //通过id获取统一登录用户
    this.getByUid = function(userid,success_func,error_func){
        var resource = $resource(sso_baseurl+"user/getinfo/byuserid/:userid");
        return resource.get({userid:userid},success_func,error_func)
    };
    //绑定手机
    this.bindPhone = function(data,success_func,error_func){
        var resource = $resource(sso_baseurl+"user/binding/phone");
        this.update(data);
        return resource.save({},data,success_func,error_func);
    };
    //更新外卖用户信息
    this.update = function(data){
        var resource = $resource(uc_baseurl+"user/:userid");
        return resource.save({userid:data.userid},data,function(){
        },function(){
            console.log("外卖用户更新失败");
        });
    };
    //获取外卖用户
    this.getxwmUser = function(userid){
        var uc_user = $resource(uc_baseurl+"user/:userid");
        //p判断外卖是否有用户没有用户添加一个新的用户
        return uc_user.get({userid:userid});
    };
    //外卖平台添加新的用户
    this.addxwmUser = function(xwmuser){
        var user = {
            userid:xwmuser.userid,
            nickname:xwmuser.username,
            name:$cookies.get("openid"),
            password:$cookies.get("openid"),
            sign:21,
            outerid:10001
        };
        var xwmRegist = $resource(uc_baseurl+'user/add');
        return xwmRegist.save({userid:user.userid},user);
    };
    //通过openid解除微信绑定
    this.unbinding = function(openid,success,error){
        var appid = $cookies.get("appid");
        var resource = $resource(sso_baseurl+"user/wxuser/unbinding/:unionid");
        return resource.save({unionid:openid,appid:appid},{},success,error);
    }
    //微信游客注册
    this.wxvisitor = function(visitorUser,success,error){
        var resource = $resource(sso_baseurl+"user/wxvisitor");
        return resource.save(visitorUser,success,error);
    }
}